package limeirafol.gui.Cadastro;

import java.awt.event.KeyEvent;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.SwingConstants;
import limeirafol.bean.BeanCadastro;
import limeirafol.remote.cadastro.UsuarioFiltroConsultaVO;
import lbframework.classe.Util;
import lbframework.classe.LBException;
import lbframework.bean.internalFrame.LBInternalFrame;
import lbframework.bean.mdiFrame.LBMdiFrame;
import lbframework.bean.table.LBColumnTable;
import lbframework.remote.ItemComboVO;
import limeirafol.Global;
import limeirafol.remote.Formulario;
import limeirafol.remote.TipoOperacao;
import limeirafol.remote.UsuarioVO;

public class UsuarioConsultaGUI extends LBInternalFrame {

    private BeanCadastro beancadastro = new BeanCadastro();
    private JButton btnImprimir;
    private Vector<UsuarioVO> vUsuario = null;
    private UsuarioFiltroConsultaVO oFiltro = new UsuarioFiltroConsultaVO();

    public UsuarioConsultaGUI(LBMdiFrame i_mdiFrame) throws Exception {
        super(i_mdiFrame);
        initComponents();

        centralizarForm();
        this.setMaximum(true);

        cboSituacao.setTabela("situacaocadastro");
        cboSituacao.carregar();
        cboSituacao.addItem(new ItemComboVO(-1, "TODOS"));

    }

    @Override
    public void consultar() throws Exception {
        Global.verificarPermissao(Formulario.CADASTRO_USUARIO, TipoOperacao.CONSULTAR);

        oFiltro = new UsuarioFiltroConsultaVO();
        oFiltro.id = txtCodigo.getInt();
        oFiltro.login = txtLogin.getText();
        oFiltro.nome = txtNome.getText();
        oFiltro.idSituacaoCadastro = cboSituacao.getId();
       
        vUsuario = beancadastro.consultarUsuario(oFiltro);

        exibirConsulta();
    }

    public void exibirConsulta() throws Exception {
        LBColumnTable[] colunas = new LBColumnTable[]{
            new LBColumnTable("Código", 70, true, SwingConstants.LEFT, false, null),
            new LBColumnTable("Nome", 220, true, SwingConstants.LEFT, false, null),
            new LBColumnTable("Login", 120, true, SwingConstants.LEFT, false, null),
            new LBColumnTable("Situação", 90, true, SwingConstants.LEFT, false, null),
            new LBColumnTable("Último Acesso", 120, true, SwingConstants.LEFT, false, null)
        };

        Object[][] dados = new Object[vUsuario.size()][colunas.length];

        int i = 0;

        for (UsuarioVO oUsuario : vUsuario) {
            dados[i][0] = Util.formatNumber(oUsuario.id, 6);
            dados[i][1] = oUsuario.nome;
            dados[i][2] = oUsuario.login;
            dados[i][3] = oUsuario.situacaoCadastro;
            dados[i][4] = oUsuario.dataHoraUltimoAcesso;

            i++;
        }

        tblConsulta.setModel(dados, colunas);

        tblConsulta.requestFocus();
    }

   

    @Override
    public void incluir() throws Exception {
        Global.verificarPermissao(Formulario.CADASTRO_USUARIO, TipoOperacao.INCLUIR);

        UsuarioCadastroGUI form = new UsuarioCadastroGUI(mdiFrame);

        form.incluir();
        form.setVisible(true);
    }

    private void carregarUsuario() throws Exception {
        if (tblConsulta.getLinhaSelecionada() == -1) {
            throw new LBException(Util.MSG_NENHUM_ITEM_SELECIONADO);
        }

        UsuarioVO oUsuario = vUsuario.get(tblConsulta.getLinhaSelecionada());

        if (consultaCampo == null) {
            UsuarioCadastroGUI form = new UsuarioCadastroGUI(mdiFrame);

            form.carregarUsuario(oUsuario.id);
            form.setVisible(true);

        } else {
            this.setVisible(false);

            consultaCampo.setParentSelected(true);
            consultaCampo.setId(oUsuario.id);
            consultaCampo.consultar(true);
            consultaCampo.transferFocus();
        }
    }

    @Override
    public void editar() throws Exception {
        if (tblConsulta.getLinhaSelecionada() == -1) {
            throw new LBException(Util.MSG_NENHUM_ITEM_SELECIONADO);
        }

        UsuarioVO oUsuario = vUsuario.get(tblConsulta.getLinhaSelecionada());

        UsuarioCadastroGUI form = new UsuarioCadastroGUI(mdiFrame);

        form.carregarUsuario(oUsuario.id);
        form.setVisible(true);
    }

    @Override
    public void excluir() throws Exception {
       Global.verificarPermissao(Formulario.CADASTRO_USUARIO, TipoOperacao.EXCLUIR);

        if (tblConsulta.getLinhaSelecionada() == -1) {
            throw new LBException(Util.MSG_NENHUM_ITEM_SELECIONADO);
        }

        Util.exibirMensagemConfirmar(Util.MSG_CONFIRMA_EXCLUSAO, getTitle());

        UsuarioVO oUsuario = vUsuario.get(tblConsulta.getLinhaSelecionada());

        beancadastro.excluirUsuario(oUsuario.id);

        vUsuario.remove(oUsuario);

        exibirConsulta();
    }

   

    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        popImpressao = new javax.swing.JPopupMenu();
        btnImprimirPermissao = new javax.swing.JMenuItem();
        btnImprimirUsuario = new javax.swing.JMenuItem();
        vRPanel1 = new lbframework.bean.panel.LBPanel();
        vRLabel1 = new lbframework.bean.label.LBLabel();
        txtLogin = new lbframework.bean.textField.LBTextField();
        vRLabel3 = new lbframework.bean.label.LBLabel();
        vRLabel6 = new lbframework.bean.label.LBLabel();
        cboSituacao = new lbframework.bean.comboBox.LBComboBox();
        txtCodigo = new lbframework.bean.textField.LBTextField();
        vRLabel7 = new lbframework.bean.label.LBLabel();
        txtNome = new lbframework.bean.textField.LBTextField();
        vRPanel2 = new lbframework.bean.panel.LBPanel();
        btnConsultar = new lbframework.bean.button.LBButton();
        btnSair = new lbframework.bean.button.LBButton();
        tlbToolBarPadrao = new lbframework.bean.toolBarPadrao.LBToolBarPadrao(this);
        tblConsulta = new lbframework.bean.tableEx.LBTableEx();

        btnImprimirPermissao.setText("Permissão");
        btnImprimirPermissao.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnImprimirPermissaoActionPerformed(evt);
            }
        });
        popImpressao.add(btnImprimirPermissao);

        btnImprimirUsuario.setText("Usuário");
        btnImprimirUsuario.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnImprimirUsuarioActionPerformed(evt);
            }
        });
        popImpressao.add(btnImprimirUsuario);

        setTitle("Consulta de Usuário");
        setFrameIcon(new javax.swing.ImageIcon(getClass().getResource("/lbframework/img/atalho/usuario.png"))); // NOI18N

        vRLabel1.setText("Código");

        txtLogin.setColumns(12);

        vRLabel3.setText("Login");

        vRLabel6.setText("Situação");

        txtCodigo.setColumns(6);
        txtCodigo.setMascara("Numero");

        vRLabel7.setText("Nome");

        txtNome.setColumns(12);

        javax.swing.GroupLayout vRPanel1Layout = new javax.swing.GroupLayout(vRPanel1);
        vRPanel1.setLayout(vRPanel1Layout);
        vRPanel1Layout.setHorizontalGroup(
            vRPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(vRPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(vRPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(vRLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtCodigo, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(vRPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(vRLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtNome, javax.swing.GroupLayout.PREFERRED_SIZE, 135, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(vRPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(vRLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtLogin, javax.swing.GroupLayout.PREFERRED_SIZE, 135, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(vRPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(vRLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cboSituacao, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(246, Short.MAX_VALUE))
        );
        vRPanel1Layout.setVerticalGroup(
            vRPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(vRPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(vRPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(vRPanel1Layout.createSequentialGroup()
                        .addComponent(vRLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cboSituacao, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(vRPanel1Layout.createSequentialGroup()
                        .addComponent(vRLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 15, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(26, 26, 26))
                    .addComponent(txtLogin, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(vRPanel1Layout.createSequentialGroup()
                        .addComponent(vRLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, 15, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(26, 26, 26))
                    .addComponent(txtNome, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(vRPanel1Layout.createSequentialGroup()
                        .addComponent(vRLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtCodigo, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(13, Short.MAX_VALUE))
        );

        btnConsultar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/lbframework/img/consultar.png"))); // NOI18N
        btnConsultar.setText("Consultar");
        btnConsultar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnConsultarActionPerformed(evt);
            }
        });

        btnSair.setIcon(new javax.swing.ImageIcon(getClass().getResource("/lbframework/img/sair.png"))); // NOI18N
        btnSair.setText("Sair");
        btnSair.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSairActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout vRPanel2Layout = new javax.swing.GroupLayout(vRPanel2);
        vRPanel2.setLayout(vRPanel2Layout);
        vRPanel2Layout.setHorizontalGroup(
            vRPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, vRPanel2Layout.createSequentialGroup()
                .addContainerGap(523, Short.MAX_VALUE)
                .addComponent(btnConsultar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnSair, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        vRPanel2Layout.setVerticalGroup(
            vRPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(vRPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(btnSair, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(btnConsultar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        tlbToolBarPadrao.setRollover(true);
        tlbToolBarPadrao.setVisibleConsultar(true);
        tlbToolBarPadrao.setVisibleEditar(true);
        tlbToolBarPadrao.setVisibleExcluir(true);
        tlbToolBarPadrao.setVisibleIncluir(true);

        tblConsulta.setHabilitaOrdenacao(true);
        tblConsulta.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tblConsultaMouseClicked(evt);
            }
        });
        tblConsulta.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                tblConsultaKeyPressed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(vRPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(vRPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(tlbToolBarPadrao, javax.swing.GroupLayout.DEFAULT_SIZE, 711, Short.MAX_VALUE)
                    .addComponent(tblConsulta, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 711, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(tlbToolBarPadrao, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(vRPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(tblConsulta, javax.swing.GroupLayout.DEFAULT_SIZE, 258, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(vRPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    private void btnConsultarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnConsultarActionPerformed
        try {
            consultar();

        } catch (Exception ex) {
            Util.exibirMensagemErro(ex, getTitle());
        }
    }//GEN-LAST:event_btnConsultarActionPerformed

    private void btnSairActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSairActionPerformed
        try {
            sair();

        } catch (Exception ex) {
            Util.exibirMensagemErro(ex, getTitle());
        }
}//GEN-LAST:event_btnSairActionPerformed

    private void tblConsultaKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tblConsultaKeyPressed
        try {
            if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
                carregarUsuario();
            }

            if (evt.getKeyCode() == KeyEvent.VK_DELETE) {
                excluir();
            }

        } catch (Exception ex) {
            Util.exibirMensagemErro(ex, getTitle());
        }
    }//GEN-LAST:event_tblConsultaKeyPressed

    private void tblConsultaMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblConsultaMouseClicked
        try {
            if (evt.getClickCount() == 2) {
                carregarUsuario();
            }

        } catch (Exception ex) {
            Util.exibirMensagemErro(ex, getTitle());
        }
    }//GEN-LAST:event_tblConsultaMouseClicked

    private void btnImprimirUsuarioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnImprimirUsuarioActionPerformed
     
}//GEN-LAST:event_btnImprimirUsuarioActionPerformed

    private void btnImprimirPermissaoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnImprimirPermissaoActionPerformed
      
}//GEN-LAST:event_btnImprimirPermissaoActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private lbframework.bean.button.LBButton btnConsultar;
    private javax.swing.JMenuItem btnImprimirPermissao;
    private javax.swing.JMenuItem btnImprimirUsuario;
    private lbframework.bean.button.LBButton btnSair;
    private lbframework.bean.comboBox.LBComboBox cboSituacao;
    private javax.swing.JPopupMenu popImpressao;
    private lbframework.bean.tableEx.LBTableEx tblConsulta;
    private lbframework.bean.toolBarPadrao.LBToolBarPadrao tlbToolBarPadrao;
    private lbframework.bean.textField.LBTextField txtCodigo;
    private lbframework.bean.textField.LBTextField txtLogin;
    private lbframework.bean.textField.LBTextField txtNome;
    private lbframework.bean.label.LBLabel vRLabel1;
    private lbframework.bean.label.LBLabel vRLabel3;
    private lbframework.bean.label.LBLabel vRLabel6;
    private lbframework.bean.label.LBLabel vRLabel7;
    private lbframework.bean.panel.LBPanel vRPanel1;
    private lbframework.bean.panel.LBPanel vRPanel2;
    // End of variables declaration//GEN-END:variables
}
